package com.intellagric.controller.module.agriecom_news;

import com.intellagric.agriecom.module.agriecom_news.NewsService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.pojo.AgriecomNews;
import com.intellagric.service.search.agriecom.AgriecomNewsSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.jms.*;
import java.util.Date;
import java.util.UUID;

/**
 * @Autuor cky
 * @Date 2018/11/6 16:02
*/
@Controller
@RequestMapping("news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Destination newsTopic;

    @Autowired
    private AgriecomNewsSearch newsSearch;

    /**
     * 根据id查询一条新闻
     * @param id
     * @return message("news",news);
     */
    @RequestMapping(value="{id}")
    @ResponseBody
    public ResponseMessage findNewsById(@PathVariable String id){
        AgriecomNews news=newsService.selectNewsById(id);
        if(news==null){         //获取异常
            return ResponseMessage.fail();
        }
        ResponseMessage msg=new ResponseMessage();
        msg.add("news",news);
        msg.setCode(200);
        msg.setTotal(1);
        return msg;    //成功

    }

    /**
     * 添加新闻信息
     * @param news
     * @return ResponseMessage
     */
    @RequestMapping("add")
    @ResponseBody
    public ResponseMessage addNews(final AgriecomNews news){
        if(news!=null) {
            news.setId(UUID.randomUUID().toString().replace("-",""));
            final String newsId=news.getId();
            if(news.getDelFlag()==null||news.getDelFlag().equals(""))
                news.setDelFlag("0");
            news.setCreateDate(new Date(System.currentTimeMillis()));
            news.setUpdateDate(new Date(System.currentTimeMillis()));

            ResponseMessage msg= newsService.insertNews(news);
            jmsTemplate.send(newsTopic,new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    TextMessage textMessage=session.createTextMessage(newsId);
                    return textMessage;
                }
            });
            return msg;
        }
        return ResponseMessage.fail("插入数据为空");

    }

    /**
     * 删除新闻信息
     * @Param id
     * @return ResponseMessage
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResponseMessage delete(String id) {
        return newsService.deleteNews(id);
    }
    /**
     * 批量删除新闻信息
     * @Param id
     * @return ResponseMessage
     */
    @RequestMapping(value="deleteAll",method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage deleteAll(@RequestParam(name = "ids[]") String[] ids) {
        for(String id:ids) {
            ResponseMessage message = newsService.deleteNews(id);
            if (message.getCode() == 100)
                return ResponseMessage.fail("删除异常");
        }
        return ResponseMessage.success();
    }

    /**
     * 编辑修改新闻信息
     * @Param news
     * @return ResponseMessage
     */
    @RequestMapping("edit")
    @ResponseBody
    public ResponseMessage edit(AgriecomNews news) {

        if(news.getId()==null||news.getId().equals("")){
            return this.addNews(news);
        }
        return newsService.editNews(news);
    }

    /**
     * 分页查询(restful)
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("list/{page}&{limit}")
    @ResponseBody
    public LayuiDataGridResult query(@PathVariable(value="page") int page,@PathVariable("limit") int rows){
        return newsService.getList(page,rows);
    }
    /**
     * 分页查询
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public LayuiDataGridResult queryList(@RequestParam(name="page",defaultValue = "1") int page,@RequestParam(name = "limit", defaultValue="20") int limit){
        return newsService.getList(page,limit);
    }

    /**
     * 根据类型id查询列表
     * @param newsTypeId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("list/{newsTypeId}")
    @ResponseBody
    public  LayuiDataGridResult queryListByType(@PathVariable("newsTypeId") String newsTypeId,@RequestParam(name="page" ,defaultValue = "1") int page,@RequestParam(name="limit",defaultValue = "15") int limit){
        return newsService.getListByType(newsTypeId,page,limit);
    }

    @RequestMapping("/addAllToIndex")
    @ResponseBody
    public ResponseMessage addAllToIndex(){
        newsSearch.getNewsList();
        return ResponseMessage.success();
    }

    /**
    * @Author:         Ade
    * @CreateDate:     2019/3/2 18:36
    * 根据新闻标题
    */
    @RequestMapping("newsList")
    @ResponseBody
    public LayuiDataGridResult query(int page,int limit,String keyword){
        return   newsService.getNewsListByKeyword(page,limit,keyword);
    }




}
